package com.sm.healthy.ui.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.sm.healthy.R;
import com.sm.healthy.data.bean.MoveBean;

import java.util.List;


/**
 * Created by sm on 2018/12/28.
 */

public class DrawLineView extends View {

    private static final String TAG = "DrawLineView";
    //画胎监连线图1
    private Paint paint1 = new Paint();
    //画胎监连线图2
    private Paint paint2 = new Paint();

    private List<MoveBean> moveData;//绘制波形Y轴数据
    private List<MoveBean> moveDataFHR2;//绘制波形Y轴数据
    private int moveWidth;//当前表格的实际可位移宽度
    private int startX = 0;//绘制波形起始位置
    private float lineSize = 2f;//绘制波形线条粗细值
    private boolean openPath2 = false;
    private float drawHeight;
    private float movetTime;
    private float errorHeight;
    private List<MoveBean> downList;
    private int mWidth;
    private Bitmap downBitmap;
    private Bitmap topBitmap;

    public DrawLineView(Context context) {
        super(context);
    }

    public DrawLineView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawLineView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int widthsize = mWidth > 0 ? mWidth : MeasureSpec.getSize(widthMeasureSpec);      //取出宽度的确切数值
        int widthmode = MeasureSpec.getMode(widthMeasureSpec);      //取出宽度的测量模式
        int heightsize = MeasureSpec.getSize(heightMeasureSpec);    //取出高度的确切数值
        int heightmode = MeasureSpec.getMode(heightMeasureSpec);    //取出高度的测量模式
        setMeasuredDimension(widthsize, heightsize);
        mWidth = widthsize;
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    private int count = 1;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint1.setStyle(Paint.Style.STROKE);
        paint1.setDither(true);
        paint1.setAntiAlias(true);
        paint1.setColor(Color.BLACK);
        paint1.setStrokeWidth(lineSize);//设置画笔宽度为2px
        if (moveData != null && moveData.size() > 0) {
            for (int i = 0; i < moveData.size(); i++) {
                if (moveData.get(i).moveY < drawHeight) {
                    canvas.drawLine(
                            moveData.get(i).moveX, moveData.get(i).moveY,
                            moveData.get(i).moveX + 1f, moveData.get(i).moveY,
                            paint1);

                    count++;
                }
            }
//            path.reset();
        }

        if (openPath2) {
            if (moveDataFHR2 != null && moveDataFHR2.size() > 0) {
                //                for (int i = 1; i < moveDataFHR2.size(); i++) {
                //                    if (moveDataFHR2.get(i).moveY < drawHeight) {
                //                        path2.moveTo(startX, (moveDataFHR2.get(i).moveY + 20));//画路径初始位置
                //                        break;
                //                    }
                //                }
                for (int i = 0; i < moveDataFHR2.size(); i++) {
                    if (moveDataFHR2.get(i).moveY < drawHeight) {
                        Log.e(TAG, "moveX = " + moveDataFHR2.get(i).moveX + "; moveY = " + moveDataFHR2.get(i).moveY + " ; ");
                        //                        path2.lineTo(moveDataFHR2.get(i).moveX, (moveDataFHR2.get(i).moveY + 20));
                        canvas.drawLine(
                                moveDataFHR2.get(i).moveX, moveDataFHR2.get(i).moveY,
                                moveDataFHR2.get(i).moveX + 3.0f, moveDataFHR2.get(i).moveY,
                                paint1);
                    }
                }
                //                canvas.drawPath(path2, paint1);
                //                path2.reset();
            }
        }


        paint2.setColor(ContextCompat.getColor(getContext(), R.color.black));       //设置画笔颜色
        paint2.setStyle(Paint.Style.FILL);  //设置画笔模式为填充
        paint2.setStrokeWidth(2f);         //设置画笔宽度为10px
        paint2.setAntiAlias(true);//抗锯齿
        paint2.setDither(true);//防抖动
        if (downList != null && topBitmap != null && downBitmap != null) {
            for (MoveBean moveBean : downList) {
                if (moveBean.isAuto == 1) {
                    canvas.drawBitmap(topBitmap, moveBean.moveX, moveBean.moveY, paint2);
                    //canvas.drawCircle(moveBean.moveX, moveBean.moveY, 6f, paint2);
                } else if (moveBean.isAuto == 2) {
                    canvas.drawBitmap(downBitmap, moveBean.moveX, moveBean.moveY, paint2);
                    //canvas.drawCircle(moveBean.moveX, moveBean.moveY, 3f, paint2);
                } else {
                    canvas.drawBitmap(topBitmap, moveBean.moveX, moveBean.moveY, paint2);
                    if (!TextUtils.isEmpty(moveBean.content)) {
                        drawCellText(canvas, moveBean.moveX + 10f, moveBean.moveY, 8, moveBean.content);
                    }
                }
            }
        }
    }

    Path path = new Path();//初始化Path
    Path path2 = new Path();//初始化Path

    public void resetPath() {
        path.reset();
        path2.reset();
    }

    public void setMyBitmap(Bitmap topBitmap, Bitmap downBitmap) {
        this.topBitmap = topBitmap;
        this.downBitmap = downBitmap;
    }

    // 绘制单元格中的文字
    private void drawCellText(Canvas canvas, float textX, float textY, int textSize, String text) {
        Paint paint = new Paint();
        paint.setFlags(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.GRAY);
        paint.setTextSize(textSize);
        canvas.drawText(text, textX, textY, paint);
    }

    /**
     * 胎心率1
     *
     * @param moveData
     */
    public void setMoveData(List<MoveBean> moveData) {
        this.moveData = moveData;
        invalidate();
    }

    /**
     * 胎心率2
     *
     * @param moveDataFHR2
     */
    public void setMoveDataFHR2(List<MoveBean> moveDataFHR2) {
        this.moveDataFHR2 = moveDataFHR2;
        invalidate();
    }

    /**
     * 设置最大位移宽度
     *
     * @param moveWidth
     */
    public void setMoveWidth(int moveWidth) {
        this.moveWidth = moveWidth;
    }

    /**
     * 设置每次位移宽度
     *
     * @param movetTime
     */
    public void setMoveTime(float movetTime) {
        this.movetTime = movetTime;
    }

    /**
     * 获取可绘制总高度
     *
     * @param drawHeight
     */
    public void setCanDrawHeight(float drawHeight) {
        this.drawHeight = drawHeight;
    }

    /**
     * 获取绘制误差高度
     *
     * @param errorHeight
     */
    public void setDrawHeightError(float errorHeight) {
        this.errorHeight = errorHeight;
    }

    /**
     * 设置监测画笔粗细
     */
    public void setPaintStyle(float lineSize) {
        this.lineSize = lineSize;
    }

    /**
     * 是否显示胎心率2
     *
     * @param openPath2
     */
    public void setPath2Open(boolean openPath2) {
        this.openPath2 = openPath2;
    }

    //    int lastX;
    //    int lastY;
    //    public boolean onTouchEvent(MotionEvent event) {
    //        //获取到手指处的横坐标和纵坐标
    //        int x = (int) event.getX();
    //        int y = (int) event.getY();
    //
    //        switch (event.getAction()) {
    //            case MotionEvent.ACTION_DOWN:
    //
    //                lastX = x;
    //                lastY = y;
    //                break;
    //            case MotionEvent.ACTION_MOVE:
    //                //计算移动的距离
    //                int offX = x - lastX;
    //                int offY = y - lastY;
    //                //调用layout方法来重新放置它的位置
    //                layout(getLeft() + offX, getTop() + offY, getRight() + offX, getBottom() + offY);
    //                break;
    //        }
    //        return true;
    //    }

    public void setTagText(List<MoveBean> downList) {
        this.downList = downList;
        invalidate();
    }

    public int getDrawWidth() {
        return getWidth();
    }
}
